package com.bootdo.system.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.bootdo.system.domain.UserpicDO;
import com.bootdo.system.service.UserpicService;
import com.bootdo.common.config.BootdoConfig;
import com.bootdo.common.config.Constant;
import com.bootdo.common.utils.FileUtil;
import com.bootdo.common.utils.OssUtil;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.ShiroUtils;

/**
 * 
 * @ClassName: UserpicController
 * @Description: TODO(用户头像图片)
 * @author lzy
 * @date 2018年6月6日
 *
 */

@Controller
@RequestMapping("/system/userpic")
public class UserpicController {
	@Autowired
	private UserpicService userpicService;

	@GetMapping()
	@RequiresPermissions("system:userpic:userpic")
	String Userpic() {
		return "system/userpic/userpic";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:userpic:userpic")
	public PageUtils list(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);
		List<UserpicDO> userpicList = userpicService.list(query);
		int total = userpicService.count(query);
		PageUtils pageUtils = new PageUtils(userpicList, total);
		return pageUtils;
	}

	@GetMapping("/add")
	@RequiresPermissions("system:userpic:add")
	String add() {
		return "system/userpic/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("system:userpic:edit")
	String edit(@PathVariable("id") Integer id, Model model) {
		UserpicDO userpic = userpicService.get(id);
		model.addAttribute("userpic", userpic);
		return "system/userpic/edit";
	}

	/**
	 * 查询头像
	 * 
	 * @param userpic
	 * @return
	 */
	@ResponseBody
	@GetMapping("/getUserpic")
	public UserpicDO getUserpic(Long userId) {
		UserpicDO user = userpicService.getUserpicByUserId(userId);
		if (user != null) {
			return user;
		} else {
			UserpicDO userpicDO = new UserpicDO();
			userpicDO.setImgurl(Constant.USER_PIC);// 设置默认头像
			return userpicDO;
		}

	}

	/**
	 * 保存头像
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save(@RequestParam("file") MultipartFile file,Long userId) {
		
			UserpicDO userpicByUserId = userpicService.getUserpicByUserId(userId);
			if (userpicByUserId != null) {
				UserpicDO userpicDO = new UserpicDO();
				userpicDO.setId(userpicByUserId.getId());
				String URL = null;
				try {
					URL = OssUtil.fileUpload(file);
				} catch (IOException e) {
					e.printStackTrace();
				}
				String imgUrl = userpicService.get(userpicByUserId.getId()).getImgurl().substring(45, 62);
				OssUtil.deleteBucketInfo(imgUrl);
				userpicDO.setImgurl(URL);
				userpicService.update(userpicDO);
				return R.ok(URL);

			} else {
				UserpicDO user = new UserpicDO();
				String URL = null;
				try {
					URL = OssUtil.fileUpload(file);
				} catch (IOException e2) {
					e2.printStackTrace();
				}
				user.setImgurl(URL);
				user.setUserId(userId);
				if (userpicService.save(user) > 0) {
					return R.ok(URL);
				}else {
					return R.error();
				}
			}
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:userpic:edit")
	public R update(UserpicDO userpic) {
		userpicService.update(userpic);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ResponseBody
	@RequiresPermissions("system:userpic:remove")
	public R remove(Integer id) {
		if (userpicService.remove(id) > 0) {
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@PostMapping("/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:userpic:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids) {
		userpicService.batchRemove(ids);
		return R.ok();
	}

}
